package com.hewep.demo.device;

import com.hewep.demo.device.mina.core.*;
import com.hewep.demo.device.mina.core.message.BasicResponse;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ServerIoHandler extends IoHandlerAdapter {
	
	private Handler handler;
	private ActionMapping actionMapping;
	
	private static final Logger log = LoggerFactory.getLogger(ServerIoHandler.class);
	
	public ServerIoHandler(ServerConfig config) {	
		this.actionMapping = new ActionMapping(config.getRoutes());
		this.handler = new ActionHandler(actionMapping);
	}

	@Override
	public final void messageReceived(IoSession session, Object message)
			throws Exception {		
		Request request = (Request)message;
		log.info("接受到基站提交的请求:{}",request.toString());
		Response response = new BasicResponse();
		handler.handle(session,request,response);
	}
	
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {		
		super.exceptionCaught(session, cause);
		cause.printStackTrace();
	}	
	
	
}
